package com.travel.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    public static final String ORDER_QUEUE_NAME = "order.queue";
    public static final String ORDER_EXCHANGE_NAME = "order.exchange";
    public static final String ORDER_ROUTING_KEY = "order.key";
    public static final String DEAD_LETTER_QUEUE_NAME = "dead.order.queue";
    public static final String DEAD_LETTER_EXCHANGE_NAME = "dead.order.exchange";
    public static final String DEAD_LETTER_ROUTING_KEY = "dead.order.key";

    public static final String PAYMENT_SUCCESS_QUEUE_NAME = "payment.success.queue";

    @Bean
    public Queue orderQueue() {
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange 声明了队列里的死信转发到的DLX
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的 routing-key 名称
        args.put("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY);
        return new Queue(ORDER_QUEUE_NAME, true, false, false, args);
    }

    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange(ORDER_EXCHANGE_NAME);
    }

    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_ROUTING_KEY);
    }

    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DEAD_LETTER_QUEUE_NAME);
    }

    @Bean
    public Queue paymentSuccessQueue() {
        return new Queue(PAYMENT_SUCCESS_QUEUE_NAME);
    }

    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME);
    }

    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with(DEAD_LETTER_ROUTING_KEY);
    }
}
